package com.flyee.utils.load.contorl
{
	import com.flyee.utils.load.model.LoaderModel;
	import com.flyee.utils.load.processor.LoadType;
	
	import flash.events.ProgressEvent;
	
	public class LoaderManager
	{
		private static const LOADTHEARD_COUNT:uint = 1;
		private var loadArrayManager:LoaderArrayManager = new LoaderArrayManager();
		
		private static var instance:LoaderManager = null;
		public function LoaderManager()
		{
			if (instance)
			{
				throw new Error("singleton error!!!");
			}
			instance = this;
		}
		public static function getInstance():LoaderManager
		{

			if (instance == null)
			{
				instance = new LoaderManager();
			}
			return instance;
		}
		public function addLoad(loader:LoaderModel):void
		{
			this.loadArrayManager.addLoader(loader);
			loadTherd();
		}
		private function loadTherd():void
		{
			if(loadArrayManager.loadingArray.length < LoaderManager.LOADTHEARD_COUNT)
			{
				var tempLoader:LoaderModel = loadArrayManager.getBlankLoader();
				if(tempLoader)
				{
					this.loadArrayManager.addLoading(tempLoader);
					var tempTheard:LoaderTheardManager = new LoaderTheardManager(tempLoader,returnHandle,progressHandle)
				}
			}
		}
		private function progressHandle(loader:LoaderModel,e:ProgressEvent):void
		{
			if(loader.loadProgressFun != null)
			{
				loader.loadProgressFun(e)
			}
		}
		private function returnHandle(loader:LoaderModel):void
		{
			this.loadArrayManager.removeLoadingLoader(loader.loadID);
			if(loader.loadType == LoadType.ERROR_LOAD)
			{
				this.loadArrayManager.addBadLoad(loader);
			}
			else if(loader.loadType == LoadType.OVERTIME_LOAD)
			{
				
			}
			else
			{
				this.loadArrayManager.addloadedArray(loader);
			}
			loader.loadReturnFun(loader)
			loadTherd();
		}
		public function removeLoader(id:uint):LoaderModel
		{
			var temp:LoaderModel = this.loadArrayManager.removeLoader(id);
			this.loadTherd();
			return temp;
		}
		public function clear():void
		{
			this.loadArrayManager.clear();
		}
	}
}